package com.beilang.base.util;

import com.beilang.hotline.telrecord.model.TelrecordModel;
import com.beilang.hotline.telrecord.to.SearchTO;
import com.beilang.hotline.telreply.model.TelreplyModel;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;

import java.awt.*;
import java.io.IOException;
import java.util.*;
import java.util.List;


public class ReportPDFUtil {

  
    public void writePDFVReportTable(int size, int row, SearchTO searchTO, TelrecordModel telrecordModel, List replyList, Document document) {
         ReportUtil reportUtil= new ReportUtil();

        try {
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font font = new Font(bfChinese, 15, Font.NORMAL);
            Font font1 = new Font(bfChinese, 10, Font.NORMAL);

            PdfPTable table = new PdfPTable(size);
            if ("Y".equals(searchTO.getRecordTimeSelected())) {
                if (row == 0) {
                    addVCellHeader(table, ReportUtil.RECORDTIME_DESC, font);
                } else {
                    addVCellDetail(table, reportUtil.deNull(telrecordModel.getRecordTimeStr()), font1);
                }
            }
            if ("Y".equals(searchTO.getCompanyIdSelected())) {
                if (row == 0) {
                    addVCellHeader(table, ReportUtil.COMPANY_DESC, font);
                } else {
                    addVCellDetail(table, reportUtil.deNull(telrecordModel.getCompanyNameDesc()), font1);
                }
            }
            if ("Y".equals(searchTO.getRecordIdSelected())) {
                if (row == 0) {
                    addVCellHeader(table, ReportUtil.IDNO_DESC, font);
                } else {
                    addVCellDetail(table, reportUtil.deNull(telrecordModel.getRecordId()), font1);
                }
            }
            if ("Y".equals(searchTO.getPersonIdSelected())) {
                if (row == 0) {
                    addVCellHeader(table, ReportUtil.NAME_DESC, font);
                    addVCellHeader(table, ReportUtil.SEX_DESC, font);
                } else {
                    addVCellDetail(table, reportUtil.deNull(telrecordModel.getPname()), font1);
                    addVCellDetail(table, reportUtil.deNull(telrecordModel.getSexDesc()), font1);
                }
            }
            if ("Y".equals(searchTO.getTelIdSelected())) {
                if (row == 0) {
                    addVCellHeader(table, ReportUtil.RECORDNUMBER_DESC, font);
                } else {
                    addVCellDetail(table, reportUtil.deNull(telrecordModel.getRecordNumber()), font1);
                }
            }
            if ("Y".equals(searchTO.getSysIdSelected())) {
                if (row == 0) {
                    addVCellHeader(table, ReportUtil.SYSTEM_DESC, font);
                } else {
                    addVCellDetail(table, reportUtil.deNull(telrecordModel.getSysNameDesc()), font1);
                }
            }
            if ("Y".equals(searchTO.getModuleIdSelected())) {
                if (row == 0) {
                    addVCellHeader(table, ReportUtil.MODULE_DESC, font);
                } else {
                    addVCellDetail(table, reportUtil.deNull(telrecordModel.getModuleNameDesc()), font1);
                }
            }
            if ("Y".equals(searchTO.getIssueIdSelected())) {
                if (row == 0) {
                    addVCellHeader(table, ReportUtil.ISSUE_DESC, font);
                } else {
                    addVCellDetail(table, reportUtil.deNull(telrecordModel.getIssueNameDesc()), font1);
                }
            }
            if ("Y".equals(searchTO.getTitleIdSelected())) {
                if (row == 0) {
                    addVCellHeader(table, ReportUtil.TITLE_DESC, font);
                } else {
                    addVCellDetail(table, reportUtil.deNull(telrecordModel.getTitleRemarks()), font1);
                }
            }
            if ("Y".equals(searchTO.getRemarksIdSelected())) {
                if (row == 0) {
                    addVCellHeader(table, ReportUtil.RECORDREMARKS_DESC, font);
                } else {
                    addVCellDetail(table, reportUtil.deNull(telrecordModel.getRecordRemarks()), font1);
                }
            }
            if ("Y".equals(searchTO.getReasonIdSelected())) {
                if (row == 0) {
                    addVCellHeader(table,ReportUtil. RECORDREASON_DESC, font);
                } else {
                    addVCellDetail(table, reportUtil.deNull(telrecordModel.getRecordReason()), font1);
                }
            }
            if ("Y".equals(searchTO.getCreatorIdSelected())) {
                if (row == 0) {
                    addVCellHeader(table, ReportUtil.CREATOR_DESC, font);
                } else {
                    addVCellDetail(table, reportUtil.deNull(telrecordModel.getCreatorNameDesc()), font1);
                }
            }
            if ("Y".equals(searchTO.getStatusIdSelected())) {
                if (row == 0) {
                    addVCellHeader(table, ReportUtil.STATUS_DESC, font);
                } else {
                    addVCellDetail(table, reportUtil.deNull(telrecordModel.getRecordStatusDesc()), font1);
                }
            }
            if ("Y".equals(searchTO.getCurrentOwnerSelected())) {
                if (row == 0) {
                    addVCellHeader(table, ReportUtil.CURRENTOIC_DESC, font);
                } else {
                    addVCellDetail(table, reportUtil.deNull(telrecordModel.getCurOwnerNameDesc()), font1);
                }
            }
            if ("Y".equals(searchTO.getReplyIdSelected())) {
                addVListCell(replyList, table, font1, size);
            }
            if (!document.isOpen())
            {
                  document.open();
            }
            document.add(table);
        } catch (DocumentException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    public void writePDFHReportTable(TelrecordModel telrecordModel, List replyList, SearchTO searchTO, Document document, com.lowagie.text.Font font) {
        try {
            PdfPTable table = new PdfPTable(5);
            ReportUtil reportUtil= new ReportUtil();
            if ("Y".equals(searchTO.getRecordTimeSelected())) {
                addCell(table, reportUtil.deNull(telrecordModel.getRecordTimeStr()), ReportUtil.RECORDTIME_DESC, font);
            }
            if ("Y".equals(searchTO.getCompanyIdSelected())) {
                addCell(table, reportUtil.deNull(telrecordModel.getCompanyNameDesc()), ReportUtil.COMPANY_DESC, font);
            }
            if ("Y".equals(searchTO.getRecordIdSelected())) {
                addCell(table, reportUtil.deNull(telrecordModel.getRecordId()), ReportUtil.IDNO_DESC, font);
            }
            if ("Y".equals(searchTO.getPersonIdSelected())) {
                addCell(table, reportUtil.deNull(telrecordModel.getPname()), ReportUtil.NAME_DESC, font);
                addCell(table, reportUtil.deNull(telrecordModel.getSexDesc()), ReportUtil.SEX_DESC, font);
            }
            if ("Y".equals(searchTO.getTelIdSelected())) {
                addCell(table, reportUtil.deNull(telrecordModel.getRecordNumber()), ReportUtil.RECORDNUMBER_DESC, font);
            }
            if ("Y".equals(searchTO.getSysIdSelected())) {
                addCell(table, reportUtil.deNull(telrecordModel.getSysNameDesc()), ReportUtil.SYSTEM_DESC, font);
            }
            if ("Y".equals(searchTO.getModuleIdSelected())) {
                addCell(table, reportUtil.deNull(telrecordModel.getModuleNameDesc()), ReportUtil.MODULE_DESC, font);
            }
            if ("Y".equals(searchTO.getIssueIdSelected())) {
                addCell(table, reportUtil.deNull(telrecordModel.getIssueNameDesc()), ReportUtil.ISSUE_DESC, font);
            }
            if ("Y".equals(searchTO.getTitleIdSelected())) {
                addCell(table, reportUtil.deNull(telrecordModel.getTitleRemarks()),ReportUtil.TITLE_DESC, font);
            }
            if ("Y".equals(searchTO.getRemarksIdSelected())) {
                addCell(table, reportUtil.deNull(telrecordModel.getRecordRemarks()), ReportUtil.RECORDREMARKS_DESC, font);
            }
            if ("Y".equals(searchTO.getReasonIdSelected())) {
                addCell(table, reportUtil.deNull(telrecordModel.getRecordReason()), ReportUtil.RECORDREASON_DESC, font);
            }
            if ("Y".equals(searchTO.getCreatorIdSelected())) {
                addCell(table, reportUtil.deNull(telrecordModel.getCreatorNameDesc()), ReportUtil.CREATOR_DESC, font);
            }
            if ("Y".equals(searchTO.getStatusIdSelected())) {
                addCell(table, reportUtil.deNull(telrecordModel.getRecordStatusDesc()), ReportUtil.STATUS_DESC, font);
            }
            if ("Y".equals(searchTO.getCurrentOwnerSelected())) {
                addCell(table, reportUtil.deNull(telrecordModel.getCurOwnerNameDesc()), ReportUtil.CURRENTOIC_DESC, font);
            }
            if ("Y".equals(searchTO.getReplyIdSelected())) {
                addListCell(replyList, table, font);
            }

            if (!document.isOpen())
            {
                  document.open();
            }
            document.add(table);
            document.newPage();
        } catch (DocumentException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    private void addListCell(List replyList, PdfPTable table, com.lowagie.text.Font font) {
            ReportUtil reportUtil= new ReportUtil();
            if (replyList != null) {
                Iterator itor = replyList.iterator();
                int i = 1;
                while (itor.hasNext()) {
                    TelreplyModel telreplyModel = (TelreplyModel) itor.next();
                    Paragraph pragraph = new Paragraph( ReportUtil.REPLY_DES + i, font);
                    PdfPCell cell = new PdfPCell(pragraph);
                    cell.setColspan(5);
                    cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
                    table.addCell(cell);
                    addCell(table, reportUtil.deNull(telreplyModel.getReplyByDesc()), ReportUtil.REPLYBY_DESC, font);
                    addCell(table, reportUtil.deNull(telreplyModel.getReplyTimeStr()),  ReportUtil.REPLYTIME_DESC, font);
                    addCell( table, reportUtil.deNull(telreplyModel.getReplyContent()),  ReportUtil.REPLYCONTENT_DESC, font);
                    addCell(table, reportUtil.deNull(telreplyModel.getReason()),  ReportUtil.REPLYREASON_DESC, font);
                    i++;
                }

            }
    }

    private void addVListCell(List replyList, PdfPTable table, com.lowagie.text.Font font, int column) {
        int i = 1;
        ReportUtil reportUtil= new ReportUtil();
        if (replyList != null && !replyList.isEmpty()) {
            Iterator itor = replyList.iterator();
            while (itor.hasNext()) {
                TelreplyModel telreplyModel = (TelreplyModel) itor.next();
                Paragraph pragraph = new Paragraph(ReportUtil.REPLY_DES + i, font);
                PdfPCell cell = new PdfPCell(pragraph);
                cell.setColspan(column);
                table.addCell(cell);
                addVCell(table, reportUtil.deNull(telreplyModel.getReplyByDesc()), ReportUtil.REPLYBY_DESC, font, column);
                addVCell(table, reportUtil.deNull(telreplyModel.getReplyTimeStr()), ReportUtil.REPLYTIME_DESC, font, column);
                addVCell(table, reportUtil.deNull(telreplyModel.getReplyContent()), ReportUtil.REPLYCONTENT_DESC, font, column);
                addVCell(table, reportUtil.deNull(telreplyModel.getReason()), ReportUtil.REPLYREASON_DESC, font, column);
                i++;
            }
        }

    }

    private void addVCellHeader(PdfPTable table, String desc, com.lowagie.text.Font font) {
        Paragraph pragraph = new Paragraph(desc, font);
        PdfPCell cell = new PdfPCell(pragraph);
        cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
        cell.setColspan(1);
        table.addCell(cell);
    }

    private void addVCellDetail(PdfPTable table, String value, com.lowagie.text.Font font) {
        Paragraph pragraph1 = new Paragraph(value, font);
        PdfPCell cell1 = new PdfPCell(pragraph1);

        cell1.setColspan(1);
        table.addCell(cell1);
    }

    private void addCell(PdfPTable table, String value, String desc, com.lowagie.text.Font font) {
        Paragraph pragraph = new Paragraph(desc, font);

        Paragraph pragraph1 = new Paragraph(value, font);
        PdfPCell cell = new PdfPCell(pragraph);
        cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
        cell.setColspan(1);

        PdfPCell cell1 = new PdfPCell(pragraph1);
        cell1.setColspan(4);
        table.addCell(cell);
        table.addCell(cell1);
    }

    private void addVCell(PdfPTable table, String value, String desc, com.lowagie.text.Font font, int column) {
        Paragraph pragraph = new Paragraph(desc, font);
        Paragraph pragraph1 = new Paragraph(value, font);
        PdfPCell cell = new PdfPCell(pragraph);
        cell.setColspan(1);

        PdfPCell cell1 = new PdfPCell(pragraph1);
        cell1.setColspan(column - 1);
        table.addCell(cell);
        table.addCell(cell1);
    }

}
